package com.in.mobilerechargedth.adapter;

import java.util.ArrayList;
import java.util.List;

import com.in.mobilerechargedth.R;
import com.in.mobilerechargedth.model.ChannelModel;

import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.TextView;

public class ThirdScreenListViewAdapter extends BaseAdapter {

	List<ChannelModel> details;
	List<ChannelModel> original_details;
	private LayoutInflater mInflater;
	private ItemFilter mFilter = new ItemFilter();

	public ThirdScreenListViewAdapter(Context context, List<ChannelModel> details) {
		super();
		this.details = details;
		this.original_details = details;
		mInflater = LayoutInflater.from(context);
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return details.size();
	}

	@Override
	public Object getItem(int arg0) {
		// TODO Auto-generated method stub
		return details.get(arg0);
	}

	@Override
	public long getItemId(int arg0) {
		// TODO Auto-generated method stub
		return details.get(arg0).getChannelId();
	}

	@Override
	public View getView(int arg0, View v, ViewGroup arg2) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		ChannelModel detail = details.get(arg0);

		if (v == null) {
			v = mInflater.inflate(R.layout.adapter_item_third_screen, null);
			viewHolder = new ViewHolder();
            viewHolder.channel_name = (TextView) v.findViewById(R.id.txtChannelName);
            viewHolder.yes_no = (TextView) v.findViewById(R.id.txtYesNo);

			v.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) v.getTag();
		}

		viewHolder.channel_name.setText(detail.getChannelName());
		viewHolder.yes_no.setText(detail.getChannelNumber());

		return v;
	}

	private class ViewHolder {
	    TextView channel_name;
	    TextView yes_no;
	}

	public Filter getFilter() {
        return mFilter;
    }

    private class ItemFilter extends Filter {
        @Override
        protected FilterResults performFiltering(CharSequence constraint) {

            Log.e("ItemFilter", "constraint: " + constraint);
            String filterString = constraint.toString().toLowerCase();

            FilterResults results = new FilterResults();

            final List<ChannelModel> list = original_details;

            int count = list.size();
            final ArrayList<ChannelModel> nlist = new ArrayList<ChannelModel>(count);

            String filterableString ;

            for (int i = 0; i < count; i++) {
                filterableString = list.get(i).getChannelName();
                if (filterableString.toLowerCase().contains(filterString)) {
                    nlist.add(list.get(i));
                }
            }

            if (filterString.equals("")) {
                results.values = original_details;
                results.count = original_details.size();
            }
            else {
                results.values = nlist;
                results.count = nlist.size();
            }


            return results;
        }

        @SuppressWarnings("unchecked")
        @Override
        protected void publishResults(CharSequence constraint, FilterResults results) {
            details = (ArrayList<ChannelModel>) results.values;
            notifyDataSetChanged();
        }

    }
}
